import json
from pymouse import *
from pykeyboard import *
import time

m = PyMouse()
k = PyKeyboard()

midpoint_x = 960
start_x = 740
start_y = 142

op_data_json = ''

with open('step_data.json', 'r', encoding='utf-8') as f:
    op_data_json = json.load(f)


def change_window(num):
    """
    切换窗口
    :param num:  窗口排序号
    :return:  null
    """
    in_str = str(num)
    k.press_keys([k.windows_l_key, in_str])
    k.release_key(k.windows_l_key)
    k.release_key(in_str)
    time.sleep(0.3)


def mouse_move_click(op_one):
    """
    鼠标移动点击操作
    :param op_one: 操作基本对象
    :return:
    """
    print(op_one)
    times = int(op_one["times"])
    m.click(op_one["x"], op_one["y"], op_one["button"], times)
    delay = float(op_one["delay"])
    time.sleep(delay)


def move_bg(op_one):
    """
    背景移动
    :param op_one: 操作基本对象
    :return:
    """
    delay = float(op_one["delay"])
    print(op_one["hold"])
    m.press(op_one["hold"]["x"], op_one["hold"]["y"], 1)
    time.sleep(delay)
    m.move(op_one["move"]["x"], op_one["move"]["y"])
    time.sleep(delay)
    m.release(op_one["move"]["x"], op_one["move"]["y"], 1)
    time.sleep(delay)


def act_list(op_list):
    for op_for in op_list:
        if op_for["type"] == 'var':
            mouse_move_click(op_for)
        elif op_for["type"] == 'hold':
            for ti in range(0, op_for["times"]):
                move_bg(op_for["move"])
        else:
            mouse_move_click(op_data_json[op_for["type"]])


def act_one(op_data):
    act_list(op_data_json["refresh_home"])
    act_list(op_data["init"])
    for_limit = int(op_data_json["all_times"][op_data["operate_name"]])
    for i in range(0, for_limit):
        act_list(op_data["act"]["actions"])
    act_list(op_data["reward"])
    act_list(op_data["exit"])


def for_all():
    # for op_for in op_data_json["operates"]:
    for op_for in op_data_json["all_times"]:
        op_name = op_data_json["operates"][op_for]["operate_name"]
        print("进入《{}》循环".format(op_name))
        act_one(op_data_json["operates"][op_for])


def one_act(field):
    op_name = op_data_json["operates"][field]["operate_name"]
    print("进入《{}》循环".format(op_name))
    act_one(op_data_json["operates"][field])


def run():
    change_window(1)
    time.sleep(1)
    sta = time.time()
    # one_act("巅峰")
    if op_data_json["operate_type"] == "所有":
        print("执行所有")
        for_all()
    else:
        one_act(op_data_json["operate_type"])
    sta = time.time() - sta
    print("总共使用{}毫秒".format(str(sta)))


run()


